﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DiskGalleryDataAPI.DataAccesss;

namespace DiskGallery
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public int DiskId;

        protected void Page_Load(object sender, EventArgs e)
        {
            DiskId = Convert.ToInt32(Request.Params["id"]);
            Session["id"] = DiskId;
            if (Session["user"] == null)
            {
                Response.Redirect("Default.aspx");
            }

            if (DiskId != 0)
            {
                if (!IsPostBack)
                {
                    DiskDataAccessor diskContent = new DiskDataAccessor();
                    DataRow disk = diskContent.SelectById(DiskId);
                    txtLabel.Text = disk["disk_label"].ToString();
                    txtAuthor.Text = disk["disk_author"].ToString();
                    txtContentName.Text = disk["disk_content_name"].ToString();
                    txtContent.Text = disk["disk_content"].ToString();
                    txtDateCreation.Text = disk["disk_date_create"].ToString();
                    txtSize.Text = disk["disk_size"].ToString();

                }

            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void DetailsView1_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
        {
            Response.Redirect("Default.aspx");
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid) return;

            DiskDataAccessor newDiskContent = new DiskDataAccessor();
            if (DiskId != 0)
            {
                newDiskContent.UpdateById(DiskId, txtLabel.Text, txtAuthor.Text, txtContentName.Text,
                                                        txtContent.Text, txtDateCreation.Text, txtSize.Text, "");
                Response.Redirect("Default.aspx");
            }
            else
            {
                newDiskContent.InsertNew(txtLabel.Text, txtAuthor.Text, txtContentName.Text,
                                                        txtContent.Text, txtDateCreation.Text, txtSize.Text, "");
                Response.Redirect("Default.aspx");
            }
        }

        protected void btCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DiskDataAccessor newDiskContent = new DiskDataAccessor();
            newDiskContent.DeleteById(DiskId);
            Response.Redirect("Default.aspx");
        }

        protected void txtLabel_TextChanged(object sender, EventArgs e)
        {
            txtLabel.DataBind();
        }

        protected void txtAuthor_TextChanged(object sender, EventArgs e)
        {
            txtAuthor.DataBind();
        }

        protected void txtContentName_TextChanged(object sender, EventArgs e)
        {
            txtContentName.DataBind();
        }

        protected void txtContent_TextChanged(object sender, EventArgs e)
        {
            txtContent.DataBind();
        }

        protected void txtDateCreation_TextChanged(object sender, EventArgs e)
        {
            txtDateCreation.DataBind();
        }

        protected void txtSize_TextChanged(object sender, EventArgs e)
        {
            txtSize.DataBind();
        }

    }
}
